package com.sjy.project.ent.yearSta.service.impl;

import java.util.List;
import com.sjy.common.utils.DateUtils;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sjy.project.ent.yearSta.mapper.EntKpiYearStaMapper;
import com.sjy.project.ent.yearSta.mapper.EntRentInLandMapper;
import com.sjy.project.ent.yearSta.mapper.EntRentOutLandMapper;
import com.sjy.project.ent.yearSta.domain.EntKpiYearSta;
import com.sjy.project.ent.yearSta.domain.EntRentInLand;
import com.sjy.project.ent.yearSta.domain.EntRentOutLand;
import com.sjy.project.ent.yearSta.service.IEntRentInLandService;
import com.sjy.common.utils.text.Convert;

/**
 * 承租土地Service业务层处理
 * 
 * @author ljb
 * @date 2022-06-07
 */
@Service
public class EntRentInLandServiceImpl implements IEntRentInLandService 
{
    @Autowired
    private EntRentInLandMapper entRentInLandMapper;
    @Autowired
    private EntRentOutLandMapper entRentOutLandMapper;
    @Autowired
    private EntKpiYearStaMapper entKpiYearStaMapper;

    /**
     * 查询承租土地
     * 
     * @param id 承租土地主键
     * @return 承租土地
     */
    @Override
    public EntRentInLand selectEntRentInLandById(Long id)
    {
        return entRentInLandMapper.selectEntRentInLandById(id);
    }

    /**
     * 查询承租土地列表
     * 
     * @param entRentInLand 承租土地
     * @return 承租土地
     */
    @Override
    public List<EntRentInLand> selectEntRentInLandList(EntRentInLand entRentInLand)
    {
        return entRentInLandMapper.selectEntRentInLandList(entRentInLand);
    }

    /**
     * 新增承租土地
     * 
     * @param entRentInLand 承租土地
     * @return 结果
     */
    @Override
    public int insertEntRentInLand(EntRentInLand entRentInLand)
    {
        entRentInLand.setCreateTime(DateUtils.getNowDate());
        return entRentInLandMapper.insertEntRentInLand(entRentInLand);
    }


    /**
     * 同步承租土地信息到对方出租土地去
     * 
     * @param entRentInLand 承租土地
     * @return 结果
     */
    public int copyIn2Out(EntRentInLand in) {
    	//检测当前承租土地信息对应的统代 企业 是否有审核记录，并且已经被街道确认的
    	EntKpiYearSta sta = entKpiYearStaMapper.selectEntKpiYearStaByCreditCode(in.getLessorCreditCode(), in.getDataYear());
    	if(sta != null && sta.getConfirmFlag() == 1) {
    		//只有当有数据，且已经被确认的情况下；就默认不执行
    		return 1;
    	}
    	
    	//检测当前承租土地信息有没有在对方的出租土地信息中
    	EntRentOutLand out = new EntRentOutLand();
    	out.setCreditCode(in.getLessorCreditCode());
    	out.setDataYear(in.getDataYear());
    	out.setDelFlag("0");
    	List<EntRentOutLand> outs = entRentOutLandMapper.selectEntRentOutLandList(out);
    	if(outs!= null && outs.size()!=0) {
    		return outs.size();
    	}
    	//插入
    	//out.setKpiId(in.getKpiId());
    	out.setCreateBy(in.getCreateBy());
    	out.setFilePath(in.getFilePath());
    	out.setEntName(in.getLessor());
    	out.setLandLocated(in.getLandLocated());
    	out.setLessee(in.getEntName());
    	out.setLesseeCreditCode(in.getCreditCode());
    	
    	out.setRentalArea(in.getLeasedArea());
    	out.setRentalAreaStreet(in.getLeasedAreaStreet());
    	out.setRentalAreaSystem(in.getLeasedAreaSystem());
    	
    	out.setAreaOfOutLand(in.getAreaOfInLand());
    	out.setAreaOfOutLandStreet(in.getAreaOfInLandStreet());
    	out.setAreaOfOutLandSystem(in.getAreaOfInLandSystem());
    	
    	return entRentOutLandMapper.insertEntRentOutLand(out);
    }
    /**
     * 修改承租土地
     * 
     * @param entRentInLand 承租土地
     * @return 结果
     */
    @Override
    public int updateEntRentInLand(EntRentInLand entRentInLand)
    {
        entRentInLand.setUpdateTime(DateUtils.getNowDate());
        return entRentInLandMapper.updateEntRentInLand(entRentInLand);
    }

    /**
     * 批量删除承租土地
     * 
     * @param ids 需要删除的承租土地主键
     * @return 结果
     */
    @Override
    public int deleteEntRentInLandByIds(String ids)
    {
        return entRentInLandMapper.deleteEntRentInLandByIds(Convert.toStrArray(ids));
    }
    /**
     * 根据kpiId批量假删
     * @param kpiId
     * @param loginName
     * @return
     */
    public int deleteEntRentInLandByKpiId(Long kpiId,String loginName) {
    	return entRentInLandMapper.deleteEntRentInLandByKpiId(kpiId, loginName);
    }
    /**
     * 根据creditCode dataYear批量假删
     * @param kpiId
     * @param loginName
     * @return
     */
    public int deleteEntRentInLandByCreditCode(String creditCode,Integer dataYear,String loginName) {

    	return entRentInLandMapper.deleteEntRentInLandByCreditCode(creditCode, dataYear, loginName);
    }
    /**
     * 删除承租土地信息
     * 
     * @param id 承租土地主键
     * @return 结果
     */
    @Override
    public int deleteEntRentInLandById(Long id)
    {
        return entRentInLandMapper.deleteEntRentInLandById(id);
    }
}
